﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TomatoClock.Models;


namespace TomatoClock.Views
{
    public partial class Rankings : Form
    {
        private Ranking ranking;
        private List<User> userlist;
        public Rankings()
        {
            InitializeComponent();
            this.ranking = new Ranking();
            PopulateRankings();
        }

        private void PopulateRankings()
        {
            var sortedList = ranking.GetFocusTimeDailyRankings(userlist);

            dataGridView1.Rows.Clear();
            foreach (var user in sortedList)
            {
                dataGridView1.Rows.Add(user.rank, user.UserName, user.data.FocusTime);
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Rankings_Load(object sender, EventArgs e)
        {

        }

        private void SearchByName_Click(object sender, EventArgs e)
        {
            string userName = textBox1.Text;
            int rank = ranking.GetRankByUserName(userlist, userName);
            if (rank != -1)
            {
                MessageBox.Show($"{userName} 的排名是：{rank}");
            }
            else
            {
                MessageBox.Show("未找到该用户");
            }
        }

        private void SearchById_Click(object sender, EventArgs e)
        {
            int rank;
            if (int.TryParse(textBox1.Text, out rank)) // 尝试将用户输入转换为整数排名
            {
                string userName = ranking.GetUserNameByRank(userlist, rank);

                if (userName != "Unknown")
                {
                    MessageBox.Show($"排名为 {rank} 的用户是：{userName}");
                }
                else
                {
                    MessageBox.Show("未找到该排名对应的用户");
                }
            }
            else
            {
                MessageBox.Show("请输入有效的排名");
            }
        }
    }
}
